]>
Commit | Line | Data |
---|---|---|
be897af3 RBR |
1 | // Copyright (C) 2024 Rubén Beltrán del Río |
2 | ||
3 | // This program is free software: you can redistribute it and/or modify | |
4 | // it under the terms of the GNU General Public License as published by | |
5 | // the Free Software Foundation, either version 3 of the License, or | |
6 | // (at your option) any later version. | |
7 | ||
8 | // This program is distributed in the hope that it will be useful, | |
9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | // GNU General Public License for more details. | |
12 | ||
13 | // You should have received a copy of the GNU General Public License | |
14 | // along with this program. If not, see https://map.tranquil.systems. | |
fdb4633d RBR |
15 | import SwiftUI |
16 | ||
17 | struct MapNotes: View { | |
18 | ||
19 | let mapSize: CGSize | |
20 | let lineWidth: CGFloat | |
21 | let notes: [Note] | |
e2c37ac1 | 22 | |
fdb4633d | 23 | let maxWidth = 400.0 |
fdb4633d RBR |
24 | |
25 | var body: some View { | |
26 | ForEach(notes, id: \.id) { note in | |
be897af3 | 27 | Text(note.text.replacingOccurrences(of: "\\n", with: "\n")).font(.Theme.note) |
fdb4633d RBR |
28 | .padding(2.0) |
29 | .background(.white) | |
be897af3 RBR |
30 | .foregroundColor(.Map.labelColor) |
31 | .border(Color.Map.vertexColor, width: lineWidth) | |
fdb4633d RBR |
32 | .frame(minWidth: 16.0, maxWidth: maxWidth, alignment: .topLeading) |
33 | .offset( | |
34 | CGSize( | |
35 | width: w(note.position.x), | |
36 | height: h(note.position.y) | |
37 | ) | |
38 | ) | |
39 | } | |
40 | } | |
41 | ||
42 | func h(_ dimension: CGFloat) -> CGFloat { | |
43 | max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0)) | |
44 | } | |
45 | ||
46 | func w(_ dimension: CGFloat) -> CGFloat { | |
47 | max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0)) | |
48 | } | |
49 | } | |
50 | ||
e2c37ac1 RBR |
51 | #Preview { |
52 | MapNotes( | |
53 | mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0, | |
54 | notes: [ | |
55 | Note( | |
56 | id: 0, position: CGPoint(x: 50.0, y: 50.0), | |
57 | text: | |
58 | "Notes can have a lot more text, so we need to make sure that they're resized correctly") | |
59 | ]) | |
fdb4633d | 60 | } |